<?php
$nev = $_SESSION['username'];
require_once 'dbconnect.php';
$lekerdezes = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE username='$nev'"));
if (isset($_POST['edit_button'])) {
    if ($_POST['edit_email'] != $lekerdezes['email']) {
        $new_email = $_POST['edit_email'];
    }
    if (!empty($_POST['edit_pass']) && !empty($_POST['edit_pass2']) && $_POST['edit_pass'] == $_POST['edit_pass2']) {
        $new_pass = md5($_POST['edit_pass']);
    }
    if ($_FILES['file']['name'] !== '') {
        $file_name = $nev . '_' . $_FILES['file']['name'];
    }
    if (isset($new_email) && isset($new_pass) && isset($file_name)) {
        mysql_query("UPDATE users SET email='$new_email', password='$new_pass', avatar='$file_name' WHERE username='$nev'");
        unlink('avatars/' . $lekerdezes['avatar']);
        move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $file_name);
    } elseif (isset($new_email) && isset($new_pass)) {
        mysql_query("UPDATE users SET email='$new_email', password='$new_pass' WHERE username='$nev'");
    } elseif (isset($new_email) && isset($file_name)) {
        mysql_query("UPDATE users SET email='$new_email', avatar='$file_name' WHERE username='$nev'");
        unlink('avatars/' . $lekerdezes['avatar']);
        move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $file_name);
    } elseif (isset($new_pass) && isset($file_name)) {
        mysql_query("UPDATE users SET password='$new_pass', avatar='$file_name' WHERE username='$nev'");
        unlink('avatars/' . $lekerdezes['avatar']);
        move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $file_name);
    } elseif (isset($new_email)) {
        mysql_query("UPDATE users SET email='$new_email' WHERE username='$nev'");
    } elseif (isset($new_pass)) {
        mysql_query("UPDATE users SET password='$new_pass' WHERE username='$nev'");
    } elseif (isset($file_name)) {
        mysql_query("UPDATE users SET avatar='$file_name' WHERE username='$nev'");
        unlink('avatars/' . $lekerdezes['avatar']);
        move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $file_name);
    }
}
?>
<div id="profile">
    <h2><?php echo $_SESSION['username']; ?></h2>
    <div id="navigator">
        <ul>
            <?php
            if (!isset($_GET['edit'])) {
                ?>
                <li class="inactive"><a href="index.php?show=settings">Megtekintés</a></li>
                <li><a href="index.php?show=settings&edit=1">Szerkesztés</a></li>
                <?php
            } else {
                ?>
                <li><a href="index.php?show=settings">Megtekintés</a></li>
                <li class="inactive"><a href="index.php?show=settings&edit=1">Szerkesztés</a></li>
                <?php
            }
            ?>
        </ul>
    </div>
    <h3>Személyes információk</h3>
    <?php
    if (isset($_GET['edit'])) {
        ?>
        <form id="edit" action="index.php?show=settings" method="post" enctype="multipart/form-data">
            <div id="dl">
                <dl>
                    <dt>E-mail cím</dt>
                    <dd>
                        <input type="text" id="edit_email" name="edit_email" value="<?php echo $lekerdezes['email']; ?>" onchange="validate_edit_email()"/>
                        <div id="email_validation"></div>
                    </dd>
                    <dt><br/>Új jelszó</dt>
                    <dd><input type="password" name="edit_pass"/></dd>
                    <dt>Megerősítés</dt>
                    <dd>
                        <input type="password" name="edit_pass2" onchange="validate_edit_pass()"/>
                        <div id="password_validation2"></div>
                    </dd>
                </dl>
            </div>
            <div id="avatar">
                <input type="file" id="file" name="file" onchange="validate_avatar()"/>
                <div id="keret"></div><div id="exist"></div>
            </div>
            <input type="submit" id="edit_button" name="edit_button" value="Mentés"/>
        </form>
        <?php
    } else {
        ?>
        <div id="dl">
            <dl>
                <dt>Felhasználónév</dt>
                <dd><?php echo $lekerdezes['username']; ?></dd>
                <dt>E-mail cím</dt>
                <dd><?php echo $lekerdezes['email']; ?></dd>
            </dl>
        </div>
        <div id="avatar">
            <img src="avatars/<?php echo $lekerdezes['avatar']; ?>"/>
        </div>
        <div id="clear"></div>
        <?php
    }
    ?>
</div>